#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include<windows.h>
#include<assert.h>//check code
#define OK 1
#define N 10
typedef struct student 
{
	int STUDENTID[2*N];//学号
	char name[2*N];//姓名string s2("Hello");
	char sex[N];//性别
	char Date[10];
	float egrade;//英语成绩
	float data_structure;//数据结构成绩
	float all_grade;//总成绩
	struct student* next;//指向下一个学生的指针
	FILE* fp;
}S;
void menu1();//teacher
void menu2();//student
void input();
void insert();
int traversal();
void Search();
void modifications();
void Delete();
void sortingorder();
void menu();
void select1();//teacher
void select2();//student
void createlist();
void menu1()//老师菜单
{
   
    system("cls");//清屏 
    int i;
    for (i = 0; i <= 25; i++)
    {
        printf(" * ");
    }
    printf("\n");
    printf("1,学生信息输入                         2，查找学生信息\n");
    printf("3,修改学生信息                         4，增加学生信息\n");
    printf("5，删除学生信息                        6，成绩排序由高到低\n");
    printf("7，遍历显示所有                        8，退出系统\n");
    printf("                  9,切换登陆系统\n                     ");
    printf("\n");
    for (i = 0; i <= 25; i++)
    {
        printf(" * ");
    }printf("\n");
    select1();
}
void menu2()//学生菜单 
{
    system("cls");
    int i;
    for (i = 0; i <= 25; i++)
    {
        printf(" * ");
    }
    printf("\n");
    printf("1,查询成绩                         2，显示所有\n");
    printf("3,成绩排行                         4，切换登陆系统\n");
    printf("                   5，退出系统                  ");
    printf("\n");
    for (i = 0; i <= 25; i++)
    {
        printf(" * ");
    }printf("\n");
    select2();
}
void menu()
{	int a;
	printf("select\n");
	scanf("%d",&a);
    printf("1.teacher\n");
    printf("2.student\n");
    switch (a)
    {
    case 1:  menu1(); break;    
    case 2:  menu2(); break;
    default: menu(); break;

    }
	
}
void select1()//teacher选择菜单 
{
    int a;
    scanf("%d", &a);
    switch (a)
    {
    case 1:input();
        break;
    case 2:Search();
        break;
    case 3:modifications();
        break;
    case 4:insert();
        break;
    case 5:Delete();
        break;
    case 6:sortingorder();
        break;
    case 7:traversal();
        break;
    case 8:printf("out"); exit(0);
        break;
    case 9:menu();
        break;
    default:menu();system("pause");
    }
}
    //选择菜单student功能;;
    void select2() 
    {
        int a;
        scanf("%d", &a);
        switch (a)
        {
        case 1:  Search(); break;
        case 2:  traversal(); break;
        default:menu(); system("pause");
        }
              
    }
//菜单
//没存进 
void input()
{	//2
  	S* head = NULL;  //头指针 
	S* p;
    FILE* fp;
    int n;
    fp = fopen("D:\\code2\\Academic Records Management System (ARMS)\\Academic Records Management System (ARMS).txt", "w+"); 
    p = (S*)malloc(sizeof(S));
   
	int i=0;
    while (10)
	{
    printf("please input your STUDENTID:\n");
    scanf("%d", &p->STUDENTID[i]);
    fprintf(fp,"%d ",p->STUDENTID[i]);
    printf("please input your name:\n");
    scanf( "%s", p->name);
    fprintf(fp,"%s ",p->name);
    printf("please input your sex:\n");
    scanf("%s", p->sex);
    fprintf(fp,"%s ",p->sex);
    printf("please input your Date:\n");
    scanf("%s", p->Date);
    fprintf(fp,"%s ",p->Date);
    printf("please input your egrade:\n");
    scanf("%f", &p->egrade);
    fprintf(fp,"%f ",p->egrade);
    printf("please input your data_structure:\n");
    scanf("%f", &p->data_structure);
    fprintf(fp,"%f ",p->data_structure);
    if (p == NULL)
    {
        printf("Memory allocation failed\n");
        fclose(fp);
        free(p);
        return menu();
    }
    else
    {
    	menu();
	}
    char temp;
    printf("是否要继续输入？");  
    scanf("%s",&temp);
    if(temp=='y'||temp=='Y')
    {
    	i++;
	}
	else
	{
		break;
	}
	
	}

	fclose(fp);
	menu();
    p->all_grade = p->egrade + p->data_structure;

    p->next = NULL;

    if (head == NULL)
    {
        head = p;
    }
    else
    {
        S* tmp = head;
        while (tmp->next!= NULL)
        {
            tmp = tmp->next;
        }
        tmp->next = p;
    }

    n = fclose(fp);
    if (n == 0)
        printf("close\n");
    
    else
    {
        printf("close error\n");
        exit(0);
    }
}


//插入
void insert()
{//1
	FILE* fp;
	fp = fopen("D:\\code2\\Academic Records Management System (ARMS)\\Academic Records Management System (ARMS).txt", "rb+");
    S* newStudent = (S*)malloc(sizeof(S));
    if (newStudent == NULL)
    {
        printf("Memory allocation failure\n");
        return;
    }

    printf("new STUDENTID: ");
    scanf("%d", &newStudent->STUDENTID[0]);
    printf("new name: ");
    scanf("%s", newStudent->name);
    printf("new sex: ");
    scanf("%s", newStudent->sex);
    printf("new Date: ");
    scanf("%s", newStudent->Date);
    printf("new english_grade: ");
    scanf("%f", &newStudent->egrade);
    printf("new data_structure: ");
    scanf("%f", &newStudent->data_structure);

    newStudent->all_grade = newStudent->egrade + newStudent->data_structure;
    newStudent->next = NULL;

    S* curr;
	S* head;
	curr=head;
    S* prev = NULL;

    while (curr!= NULL && curr->STUDENTID[0] < newStudent->STUDENTID[0])
    {
        prev = curr;
        curr = curr->next;
    }

    if (prev == NULL)
    {
        newStudent->next = head;
        head = newStudent;
    }
    else
    {
        prev->next = newStudent;
        newStudent->next = curr;
    }

    printf("insert sucsess!\n");
    fclose(fp);
}

//遍历
int traversal()
{//1

    S* p;S* head;
	p = head;
    if (p == NULL)
    {
        printf("no student\n");
        return 0;
    }
	FILE* fp;
	fp = fopen("D:\\code2\\Academic Records Management System (ARMS)\\Academic Records Management System (ARMS).txt", "rb+");
	printf("ID\t name\t sex\t Date\t egrade\t data_structure\t all_grade\t\n");
    int i;
    while (p!= NULL)
    {
        for (i = 0; i < 2 * N; i++)
        {
            fscanf(fp,"%d %s %s %s %f %f %f ",&p->STUDENTID[i], &p->name, &p->sex, &p->Date, &p->egrade, &p->data_structure, &p->all_grade); 
            printf("%d\t %s\t %s\t %s\t %.1f\t %.1f\t %.1f\n", p->STUDENTID[i], p->name, p->sex, p->Date, p->egrade, p->data_structure, p->all_grade);
        }
        p = p->next;
    }
     fclose(fp);
    return 0;
    
}

//搜索，根据学号搜索学生信息
void Search()
{//1
	FILE* fp;
    int targetID;
    printf("stuent ID: ");
    scanf("%d", &targetID);
    S* current; 
	S* head;
	head=current;
	fp = fopen("D:\\code2\\Academic Records Management System (ARMS)\\Academic Records Management System (ARMS).txt", "rb");
	int i;	
    while (current!= NULL) 
    {
        for (i = 0; i < 2 * N; i++) 
        {
            if (current->STUDENTID[i] == targetID) 
            {
                printf("find out:\n");
                printf("student ID: %d\n", current->STUDENTID[i]);
                printf("name:%s\n", current->name);
                printf("sex: %s\n", current->sex);
                printf("Date: %s\n", current->Date);
                printf("english grade: %-5.1f\n", current->egrade);
                printf("data_structure: %-5.1f\n", current->data_structure);
                printf("all_grade: %-5.1f\n", current->all_grade);
                return;
            }
        }
        current = current->next;
    }

    printf("cannot found,sorry\n");
    fclose(fp);
}
//修改
void modifications()
{	
	  S* current;
      S*head;
      current=head;
      FILE*fp;
    int targetID;
    printf("STUDENTID: ");
    scanf("%d", &targetID);
	fp = fopen("D:\\code2\\Academic Records Management System (ARMS)\\Academic Records Management System (ARMS).txt", "w+"); 
    if (fp == NULL)
    {
        printf("ERROR \n");
        exit(0);
    }
    else
    {
        printf("file open\n");
    }
  	int i;
    while (current!= NULL) 
    {
        for (i = 0; i < 2 * N; i++) 
        {
            if (current->STUDENTID[i] == targetID) 
            {
                printf("new name: ");
                scanf("%s", current->name);
                fscanf(fp,"%s", current->name);
                printf("new sex: ");
                scanf("%s", current->sex);
                fscanf(fp,"%s", current->sex);
                printf("new Date: ");
                scanf("%s", current->Date);
                fscanf(fp,"%s", current->Date);
                printf("new englishgrade: ");
                scanf("%f", &current->egrade);
                fscanf(fp,"%f", &current->egrade);
                printf("new data_structure_grade: ");
                scanf("%f", &current->data_structure);
                fscanf(fp,"%f", &current->data_structure);
                current->all_grade = current->egrade + current->data_structure;

                printf("modify sucess!\n");
                return;
            }
        }
        current = current->next;
    }

    printf("not found!\n");
    fclose(fp);
}



//删除
void Delete()
{	FILE* fp;
	S* current;
	S*head;
	current=head;
    S* prev = NULL;
    int targetID;
    printf("STUDENTID: ");
    fp = fopen("D:\\code2\\Academic Records Management System (ARMS)\\Academic Records Management System (ARMS).txt", "rb+");
    scanf("%d", &targetID);
	int i = 0;
    while (current!= NULL) 
    {
        for (i = 0; i < 2 * N; i++) 
        {
            if (current->STUDENTID[i] == targetID) 
            {
                if (prev == NULL) 
                {
                    head = current->next;
                } 
                else 
                {
                    prev->next = current->next;
                }
                free(current);
                printf("Delete susses!\n");
                return;
            }
        }
        prev = current;
        current = current->next;
    }
    printf("can not found!cannot delete!\n");
    fclose(fp);
}

//排序
void sortingorder() 
{
 /*   S *head = NULL; // ��������ȫ������ͷָ��
    S *p, *q, *temp;
	struct tempStruct;
    // ��������Ƿ�Ϊ��
    if (head == NULL)
    {
        printf("Empty chain list, no need to sort!\n");
        return;
    }

    // ð������
    for (p = head->next; p != NULL; p = p->next) {
        for (q = p->next; q != NULL; q = q->next) {
            if (p->all_grade < q->all_grade) { // ȷ���ɼ��ߵ���ǰ
                // ���� p �� q ��ָ���ѧ������
                struct tempStruct;
                memcpy(tempStruct, p, sizeof(S));
                memcpy(p, q, sizeof(S));
                memcpy(q, tempStruct, sizeof(S));
            }
        }
    }*/
}

/*
// ��������ѧ���ڵ��ָ��
void swapNodes(S** a, S** b) {
    S* temp = *a;
    *a = *b;
    *b = temp;
}

// �������������� - �ݹ麯��
void quickSortHelper(S** start, S** end) {
    if (start < end) {
        // ѡ���׼�ڵ�
        S* pivot = *end;
        S** left = start;
        S** right = start;

        while (right < end) {
            if ((*right)->all_grade > pivot->all_grade) {
                swapNodes(&(*left), &(*right));
                left++;
            }
            right++;
        }

        swapNodes(&(*left), &(pivot)); // ����׼�Ƶ���ȷ��λ��

        // �ݹ������׼��ߺ��ұߵ�������
        quickSortHelper(start, left - 1);
        quickSortHelper(left + 1, end);
    }
}

// ����������
void sortingorder(S** headRef) {
    S* head = *headRef;
    if (head != NULL && head->next != NULL) {
        quickSortHelper(&(head->next), NULL);
    }
}
*/
int main()
{
    system("color E0");
    printf("Welcome!  ");
  	menu();

}

